#ifndef MODBUS_H
#define MODBUS_H

#include <QObject>
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QMutex>
#include <QVector>
#include <QThread>
//本modbus相当于单线程的，在发送之后 3秒超时读取回复，在一个线程内完成
class Modbus:public QObject
{
    Q_OBJECT
public:
    Modbus();
    QString modbusStatus;
    QMutex locker;
    QMutex vectorLocker;
    QSerialPort port;
    QVector<int> readyRead;
    bool Open(QString portName,int baurdRate,QSerialPort::DataBits dataBits,QSerialPort::StopBits stopBits,QSerialPort::Parity parity);
    bool Close();
    void GetCRC(QByteArray message,QByteArray &Crc);
    void BuildMessage(uint8_t address, uint8_t type, ushort start, ushort registers,QByteArray &message);
    void GetResponse(QByteArray &response,int readLength);
    bool CheckResponse(QByteArray response);
    bool SendFc16(uint8_t address, ushort start, ushort registers, ushort* values);
    bool SendFc3(uint8_t address, ushort start, ushort registers,ushort *values);
    bool SendFc6(unsigned char address, ushort start, ushort registers, ushort*values);
    void ConnectSignalSlots();                                                                                                             //在modbus对象初始化成功之后调用
public slots:
    void SetComDataReady();
signals:
    void output(QByteArray);
    //添加一个一次readyRead信号成员

};


#endif // MODBUS_H
